

/** *
 * js基础单例
 */
export class ISingleton {
    public static _instance: ISingleton;

    public static GetInstance<T>(this: new () => T): T {
        if (!(this as any)._instance) {
            (this as any)._instance = new this();
            (<any>this)._instance.onInitInstance();
        }
        return (this as any)._instance;
    }

    protected onInitInstance() {
        // do something on init
    }
}

/**
 * Component 单例
 */
export class CCISingleton extends cc.Component {
    protected static _instance;

    public static GetInstance<T extends cc.Component>(this: new () => T): T {
        return (<any>this)._instance;
    }

    protected onLoad(): void {
        if ((this as any).constructor._instance) {
            console.warn(`CCISingleton ${this.name} already had instance`);
            // return;
        }
        (this as any).constructor._instance = this;
    }
}
